<template>
  <div>
    <div style="background-color: cadetblue; line-height: 32px; margin-left: 18px; font-size: 12px;">
      <div style="margin-left: 18px; font-weight: bold;">修改个人信息</div>
    </div>
    <!-- 个人信息表单 -->
    <div class="table">
      <el-form :label-position="labelPosition" :model="form" label-width="80px" style="margin-top: 20px;">
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号">
              <el-input v-model="form.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="姓名">
              <el-input v-model="form.realname" disabled></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="form.gender" align="center" disabled>
                <el-radio-button label="male">男</el-radio-button>
                <el-radio-button label="female">女</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="身份">
              <el-input v-model="form.role_name" disabled></el-input>
            </el-form-item>
            <el-form-item label="用户手机">
              <el-input v-model="form.telephone" disabled></el-input>
            </el-form-item>
            <el-form-item label="出生日期">
              <el-input :value="Number(form.birth) | fmtDate" disabled></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-input v-model="form.status" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="头像" style="margin :50px 0 0 100px;">
              <el-upload class="avatar-uploader" action="http://121.4.83.32:7006/UploadControlle/upload" disabled>
                <img :src="form.userFace" class="avatar">
              </el-upload>
              <el-button type="primary" @click="dialogTableVisible = true"
                style="margin:50px 0 0 0 ;background-color: cadetblue;">点击完善修改个人信息修改</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 模态框 -->
      <el-dialog title="修改和完善个人信息" :visible.sync="dialogTableVisible" style="margin-top:20px;" width=70%>
        <el-form v-model="form" :label-position="labelPosition">
          <el-row>
            <!-- {{ form }} -->
            <el-col :span="14">
              <el-form-item label="登录账号">
                <el-input style="width: 500px;" v-model="form.username"></el-input>
              </el-form-item>
              <el-form-item label="姓名">
                <el-input style="width: 500px;" v-model="form.realname"></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-radio-group v-model="form.gender" align="center">
                  <el-radio-button label="male">男</el-radio-button>
                  <el-radio-button label="female">女</el-radio-button>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="用户手机">
                <el-input style="width: 500px;" v-model="form.telephone"></el-input>
              </el-form-item>
              <el-form-item label="出生日期">
                <div class="block">
                  <el-date-picker v-model="form.birth" type="date" placeholder="选择日期时间">
                  </el-date-picker>
                </div>
              </el-form-item>
              <el-form-item label="状态">
                <el-input style="width: 500px;" v-model="form.status"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item prop="url" label='头像上传' style="margin-top: 40px; margin-left: 30px;">
                <el-upload class="avatar-uploader" action="http://121.4.83.32:7006/UploadControlle/upload"
                  :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button type="primary" @click="toSave" style="margin:20px 0 0 300px ">点击完善修改个人信息修改</el-button>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { get, post } from '@/utils/request'

export default {
  data() {
    return {
      form: {},
      // form 对齐方式  
      labelPosition: 'right',
      imageUrl: '',
      dialogTableVisible: false
    }
  },
  computed: {
    ...mapGetters([
      'userId',
    ]),
  },
  methods: {
    async getUserInfo() {
      this.form.id = this.userId
      console.log(this.form);
      let res = await get('/user/getUserById', { id: this.form.id })
      // console.log(res);
      this.form = res.data
    },
    // 图片上传成功的回调
    handleAvatarSuccess(res, file) {
      // console.log(res);
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.userFace = res.data
    },
    // 上传之前的回调
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // 更新个人信息
    async toSave() {
      let res = await post('/user/saveOrUpdate', this.form)
      // console.log(res);
      this.dialogTableVisible = false
      this.form = {}
    }
  },
  created() {
    this.getUserInfo()
  }
}
</script>
